package com.sky.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.sky.constant.StatusConstant;
import com.sky.dto.CategoryDTO;
import com.sky.dto.CategoryPageQueryDTO;
import com.sky.entity.Category;
import com.sky.entity.Dish;
import com.sky.entity.Setmeal;
import com.sky.mapper.DishMapper;
import com.sky.mapper.SetmealMapper;
import com.sky.result.PageResult;
import com.sky.result.Result;
import com.sky.service.CategoryService;
import com.sky.mapper.CategoryMapper;
import com.sky.utils.BeanCopyUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.util.StringUtils;

import java.util.List;
import java.util.Objects;

/**
* @author MrWang
* @description 针对表【category(菜品及套餐分类)】的数据库操作Service实现
* @createDate 2023-10-18 18:06:25
*/
@Service
public class CategoryServiceImpl extends ServiceImpl<CategoryMapper, Category>
    implements CategoryService{

    @Autowired
    private CategoryMapper categoryMapper;
    @Autowired
    private DishMapper dishMapper;
    @Autowired
    private SetmealMapper setmealMapper;

    //分类列表
    @Override
    public Result<PageResult> categoryList(CategoryPageQueryDTO categoryPageQueryDTO) {
        LambdaQueryWrapper<Category> queryWrapper = new LambdaQueryWrapper<Category>()
                .eq(Objects.nonNull(categoryPageQueryDTO.getType()),Category::getType,categoryPageQueryDTO.getType())
                .like(StringUtils.hasText(categoryPageQueryDTO.getName()),Category::getName,categoryPageQueryDTO.getName());
        Page<Category> page = new Page<>(categoryPageQueryDTO.getPage(),categoryPageQueryDTO.getPageSize());
        categoryMapper.selectPage(page,queryWrapper);
        List<Category> categoryList = page.getRecords();
        return Result.success(new PageResult(page.getTotal(),categoryList));
    }

    //添加分类
    @Transactional
    @Override
    public Result saveCategory(CategoryDTO categoryDTO) {
        //判断分类名是否重复
        LambdaQueryWrapper<Category> queryWrapper = new LambdaQueryWrapper<Category>()
                .eq(Category::getName,categoryDTO.getName());
        Category category1 = categoryMapper.selectOne(queryWrapper);
        if (category1 != null){
            return Result.error("分类名重复!!!");
        }
        Category category = BeanCopyUtils.copyBean(categoryDTO, Category.class);
        category.setStatus(StatusConstant.DISABLE);//默认是禁用
        categoryMapper.insert(category);
        return Result.success();
    }

    //删除分类
    @Transactional
    @Override
    public Result deleteById(String id) {
        //1.先判断该分类有没有关联dish表或者setmeal表记录 两个表都没有关联就可以删除该分类
        LambdaQueryWrapper<Dish> queryWrapper = new LambdaQueryWrapper<Dish>()
                .eq(Dish::getCategoryId,id);
        List<Dish> dishes = dishMapper.selectList(queryWrapper);
        LambdaQueryWrapper<Setmeal> queryWrapper1 = new LambdaQueryWrapper<Setmeal>()
                .eq(Setmeal::getCategoryId,id);
        List<Setmeal> setmeals = setmealMapper.selectList(queryWrapper1);
        if (dishes.size() != 0 || setmeals.size() != 0){
            return Result.error("该分类无法删除,已绑定菜品或套餐!!");
        }

        //2.删除该分类
        categoryMapper.deleteById(id);

        return Result.success();
    }

    //修改分类
    @Override
    public Result updateCategory(CategoryDTO categoryDTO) {
        Category category = BeanCopyUtils.copyBean(categoryDTO, Category.class);
        categoryMapper.updateById(category);
        return Result.success();
    }

    //启用、禁用分类
    @Override
    public Result updateStatus(String status,String id) {
        Category category = new Category();
        category.setId(Long.valueOf(id));
        category.setStatus(Integer.valueOf(status));
        categoryMapper.updateById(category);
        return Result.success();
    }

    //根据类型查询分类
    @Override
    public Result getCategoriesByType(String type) {
        LambdaQueryWrapper<Category> queryWrapper = new LambdaQueryWrapper<Category>()
                .eq(StringUtils.hasText(type),Category::getType,type);
        List<Category> categoryList = categoryMapper.selectList(queryWrapper);
        return Result.success(categoryList);
    }

    //C端
    //根据分类的类型查询分类列表
    @Override
    public Result getCategoryListByType(String type) {
        LambdaQueryWrapper<Category> queryWrapper = new LambdaQueryWrapper<Category>()
                .eq(Category::getStatus,StatusConstant.ENABLE)
                .eq(StringUtils.hasText(type),Category::getType,type);
        List<Category> categoryList = categoryMapper.selectList(queryWrapper);
        return Result.success(categoryList);
    }

}




